package com.yanqu.road.server.cmd.decoration;

import com.yanqu.road.entity.curio.enums.eCurioConditionType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.config.goods.MammonConfig;
import com.yanqu.road.entity.decoration.MammonRecord;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.curio.CurioModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.MammonModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.MammonConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.mammon.MammonMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_MAMMON_CONGRATULATE, desc = "恭贺财神")
public class MammonCongratulateCmd extends AbstractCommand {

	private int type;
	private String reward;
	private PlayerProto.PlayerBaseTempMsg.Builder mammonMsg;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		DecorationProto.MammonCongratulateReqMsg reqMsg = DecorationProto.MammonCongratulateReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Mammon.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		boolean isCross = MammonConfigMgr.isCrossMammonDecorationId(type)
				|| MammonConfigMgr.isCrossAreaMammonDecorationId(type);
		long recordTime = 0;
		if (player.getModule(MammonModule.class).isInCongratulateCD(type)) {
			ret = GameErrorCode.E_MAMMON_HAS_WORSHIP;
			return;
		}

		if(!isCross){
			List<MammonRecord> recordList = MammonMgr.getMammonRecordList(type);
			if(recordList.size() > 0){
				MammonRecord mammonRecord = recordList.get(recordList.size() - 1);
				recordTime = mammonRecord.getRecordTime();
				//随机一个财神爷
				List<MammonRecord> randomList = new ArrayList<>();
				for(MammonRecord tempRecord : recordList){
					if(System.currentTimeMillis() / 1000 - tempRecord.getRecordTime() <= GameConfig.MAMMON_CAN_CONGRATULATE_TIME * DateHelper.HOUR_SECONDS){
						randomList.add(tempRecord);
					}
				}
				if(randomList.size() > 0){
					RandomHelper randomHelper = new RandomHelper();
					int index = randomHelper.next(0, randomList.size());
					MammonRecord tempRecord = randomList.get(index);
					mammonMsg = PlayerBasePb.parsePlayerBaseTempMsg(tempRecord.getUserBaseInfo());
				}
				if(null == mammonMsg){
					mammonMsg = PlayerBasePb.parsePlayerBaseTempMsg(mammonRecord.getUserBaseInfo());
				}
			}
		}else {
			DecorationProto.MammonRecordListMsg.Builder listMsg = CrossDataMgr.getCrossGroupMammonRecordMsg(type);
			if(listMsg.getDataListCount() > 0){
				DecorationProto.MammonRecordTempMsg recordTempMsg = listMsg.getDataListList().get(listMsg.getDataListCount() - 1);
				recordTime = recordTempMsg.getRecordTime();
				//随机一个财神爷
				List<DecorationProto.MammonRecordTempMsg> randomMsgList = new ArrayList<>();
				for(DecorationProto.MammonRecordTempMsg tempRecord : listMsg.getDataListList()){
					if(System.currentTimeMillis() / 1000 - tempRecord.getRecordTime() <= GameConfig.MAMMON_CAN_CONGRATULATE_TIME * DateHelper.HOUR_SECONDS){
						randomMsgList.add(tempRecord);
					}
				}
				if(randomMsgList.size() > 0){
					RandomHelper randomHelper = new RandomHelper();
					int index = randomHelper.next(0, randomMsgList.size());
					DecorationProto.MammonRecordTempMsg tempRecord = randomMsgList.get(index);
					mammonMsg = tempRecord.getPlayerBaseData().toBuilder();
				}
				if(null == mammonMsg){
					mammonMsg = recordTempMsg.getPlayerBaseData().toBuilder();
				}
			}
		}

		if(0 == recordTime) {
			ret = GameErrorCode.E_MAMMON_NO_IN_RECORD;
			return;
		}
		long nowTime = System.currentTimeMillis() / 1000;
		if(nowTime - recordTime > GameConfig.MAMMON_CAN_CONGRATULATE_TIME * DateHelper.HOUR_SECONDS){
			ret = GameErrorCode.E_MAMMON_CONGRATULATE_TIME_OVER;
			return;
		}
		if(player.getModule(MammonModule.class).isMammonCongratulate(type, recordTime)){
			ret = GameErrorCode.E_MAMMON_HAS_CONGRATULATE;
			return;
		}
		MammonConfig mammonConfig = MammonConfigMgr.getMammonConfig(type);
		reward = mammonConfig.getCongratulateRewardStr();
		player.getModule(MammonModule.class).addMammonCongratulate(type, recordTime, nowTime);
		player.getModule(CurrencyModule.class).addCurrency(mammonConfig.getCongratulateReward(), eLogMoneyType.Mammon, eLogMoneyType.MammonCongratulate);
		LogMgr.addLogMammonCongratulate(player.getUserId(), recordTime + "", reward);
		//DataAnalyticsMgr.trackMammonWorshipGod(player,type);
	}

	@Override
	public void sendData(GamePlayer player) {
		DecorationProto.MammonCongratulateRespMsg.Builder respMsg = DecorationProto.MammonCongratulateRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			respMsg.setReward(reward);
			if(null != mammonMsg){
				respMsg.setPlayerBaseData(mammonMsg);
			}
		}
		player.sendPacket(Protocol.U_MAMMON_CONGRATULATE, respMsg);
	}

}
